from .exception_base import *

def retry(max_retry,logger=None):
    def decorator(func):
        count=max_retry
        def wrapper(*args,**kwargs):
            nonlocal count
            while count>0:
                try:
                    return func(*args,**kwargs)
                except StopRetry:
                    if logger:
                        logger.debug('Accept stop signal, stop retry.')
                    break
                except Exception as e:
                    count-=1
                    if logger:
                        logger.debug(f'Calling error:{e}\tRemain retry time: {count}')
            raise ExceedRetryTimeException()
        return wrapper
    return decorator

